<?php 
 header("Content-type: text/html; charset=utf-8");
require("conn.php");

$data = $_POST['data'];
$data=json_decode($data,true);


$uid = $data['uid'];

include_once "wxBizDataCrypt.php";


$appid = 'wx1998b02f7afc27de';
$sessionKey =  $data['session_key'];

$encryptedData=$data['encryptedData'];

$iv = $data['iv'];

$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data );

/*if ($errCode == 0) {
    print($data . "\n");
} else {
    print($errCode . "\n");
}*/
/*0: {timestamp: 1647964800, step: 1277}
1: {timestamp: 1648051200, step: 3299}
2: {timestamp: 1648137600, step: 695}
3: {timestamp: 1648224000, step: 1487}
4: {timestamp: 1648310400, step: 1223}
5: {timestamp: 1648396800, step: 3978}*/


//获取本周起始时间戳和结束时间戳 
$beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')); 
 $endThisweek=time();
 //获取今日开始时间戳和结束时间戳 
 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); 
 $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; 

 
$sanshi =json_decode($data, true);

$sanshi = $sanshi['stepInfoList'];
$benzhou = 0;
$todaysanbu = 0; 
for ($i=0; $i <count($sanshi); $i++) { 
$singer =$sanshi[$i];

if ($singer['timestamp'] >= $beginThisweek && $singer['timestamp']<= $endThisweek) {
	$benzhou =$benzhou + $singer['step'];//本周散步

}
if ($singer['timestamp'] == $beginToday) {
	$todaysanbu =$singer['step'];
}

}

$sql = "select * from pin where uid = ".$uid." ";
$result= mysql_query($sql,$conn );
$todayall = $todaysanbu;

$singerAct = 0;

while ($row = mysql_fetch_assoc($result)) {
	if ($row['kaishi'] > $beginThisweek) {
		$singerAct =$singerAct + $row['jifen'];//运动总积分
		if ($row['kaishi'] >$beginToday) {
			$todayall = $todayall +$row['jifen'];//今天总积分
		}
	}
}
	$benzhoujifen = $benzhou +$singerAct;//本周总积分
	$alldata  = array('today' =>$todayall ,'week' =>$benzhoujifen , );
$sql11 = "UPDATE user SET today = ".$todayall .",week=  ".$benzhoujifen ." where id ='".$uid."' ";
$result11= mysql_query($sql11,$conn);
if ($result11) {
	echo  json_encode( $alldata);
}else{
	echo "no";
}

